package cn.mooncode.basketballManage.controller;


import cn.mooncode.basketballManage.pojo.RespBean;
import cn.mooncode.basketballManage.pojo.RespPageBean;
import cn.mooncode.basketballManage.pojo.Train;
import cn.mooncode.basketballManage.pojo.TrainTeamSpace;
import cn.mooncode.basketballManage.service.ITrainService;
import cn.mooncode.basketballManage.service.ITrainTeamSpaceService;
import cn.mooncode.basketballManage.utils.UserUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jwz
 * @since 2021-03-28
 */
@RestController
@RequestMapping("/train")
public class TrainController {

    @Autowired
    private ITrainService trainService;
    @Autowired
    ITrainTeamSpaceService trainTeamSpaceService;

    @ApiOperation(value = "获取所有训练信息")
    @GetMapping("/")
    public RespPageBean getAllTrains(@RequestParam(defaultValue = "1") Integer currentPage,
                                     @RequestParam(defaultValue = "10") Integer size, Train train) {
        return trainService.getAllTrains(currentPage, size, train);
    }

    @ApiOperation(value = "获取当前用户所有训练信息")
    @GetMapping("/currentTrain")
    public RespPageBean getCurrentTrains(@RequestParam(defaultValue = "1") Integer currentPage,
                                         @RequestParam(defaultValue = "10") Integer size, Train train, @RequestParam Integer tid) {
        return trainService.getCurrentTrains(currentPage, size, train, tid);
    }

    @ApiOperation(value = "添加训练信息")
    @PostMapping("/")
    @Transactional
    public RespBean addTrain(@RequestBody Train train) {
        if (trainService.save(train)) {
            Integer tid = train.getTeamInfo().getId();
            Integer sid = train.getSpace().getId();
            Integer trid = train.getId();
            TrainTeamSpace trainTeamSpace = new TrainTeamSpace();
            trainTeamSpace.setTrid(trid);
            trainTeamSpace.setTid(tid);
            trainTeamSpace.setSid(sid);
            if (trainTeamSpaceService.save(trainTeamSpace)) {
                return RespBean.success("添加成功！");
            }
        }
        return RespBean.error("添加失败！");
    }

    @ApiOperation(value = "修改训练信息")
    @PutMapping("/")
    @Transactional
    public RespBean updateTrain(@RequestBody Train train) {
        Integer trid = train.getId();
        Integer tid = train.getTeamInfo().getId();
        Integer sid = train.getSpace().getId();
        TrainTeamSpace trainTeamSpace = new TrainTeamSpace();
        trainTeamSpace.setTrid(trid);
        trainTeamSpace.setSid(sid);
        trainTeamSpace.setTid(tid);
        if (trainService.updateById(train)) {
            if (trainTeamSpaceService.update(trainTeamSpace, new QueryWrapper<TrainTeamSpace>().eq("trid", trid))) {
                return RespBean.success("修改成功！");
            }
        }
        return RespBean.error("修改失败！");
    }

    @ApiOperation(value = "删除训练信息")
    @DeleteMapping("/{id}")
    @Transactional
    public RespBean deleteTrain(@PathVariable Integer id) {
        if (trainService.removeById(id)) {
            if (trainTeamSpaceService.remove(new QueryWrapper<TrainTeamSpace>().eq("trid", id))) {
                return RespBean.success("删除成功！");
            }
        }
        return RespBean.error("删除失败！");
    }

    @ApiOperation(value = "添加训练结果")
    @PostMapping("/addResult")
    public RespBean addResult(@RequestParam Integer id, @RequestParam String result) {
        return trainService.addResult(id, result);
    }
}
